package com.itheima.service.umsService.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.ums.UmsAdmin;
import com.itheima.domain.db.ums.UmsMenu;
import com.itheima.dto.UmsMenuDto;
import com.itheima.mapper.umsMapper.UmsMenuMapper;
import com.itheima.service.umsService.UmsMenuService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements UmsMenuService {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    //根据菜单id集合查询,菜单集合对象
    @Override
    public List<UmsMenu> findByMenuIds(List<Long> menuIds) {
        return umsMenuMapper.selectBatchIds(menuIds);
    }

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, Long parentId) {
        //1.设置分页参数
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);

        //2.创建条件查询对象-userId
        LambdaQueryWrapper<UmsMenu> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsMenu::getParentId,parentId);

        //3.调用mapper.selectPage(..)
        umsMenuMapper.selectPage(page, qw);
        Integer count = umsMenuMapper.selectCount(qw);
        //4.封装pageBeanVo返回
        return new PageBeanVo(pageNum, pageSize, Long.valueOf(count), page.getRecords());
    }

    /*
     *添加后台菜单
     * */
    @Override
    public void addUsmMenu(UmsMenu umsMenu) {
        umsMenuMapper.insert(umsMenu);
    }

    //通过id查询
    @Override
    public UmsMenu findById(Long id) {
        return umsMenuMapper.selectById(id);
    }

    //通过id删除
    @Override
    public void deleteByUsmMenuId(Long id) {
        umsMenuMapper.deleteById(id);
    }

    @Override
    public Integer findCount(Long id) {
        //2.创建条件查询对象-userId
        LambdaQueryWrapper<UmsMenu> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsMenu::getParentId,id);
        return umsMenuMapper.selectCount(qw);
    }

    @Override
    public List<UmsMenuDto> findMenuTreeList(long id) {
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new LambdaQueryWrapper<UmsMenu>().eq(UmsMenu::getParentId, 0));
        List<UmsMenuDto> umsMenuDtos = umsMenus.stream().map(umsMenu -> {
            UmsMenuDto umsMenuDto = new UmsMenuDto();
            BeanUtil.copyProperties(umsMenu, umsMenuDto);
            findChildren(umsMenuDto);
            return umsMenuDto;
        }).collect(Collectors.toList());
        return umsMenuDtos;
    }

    //树状


    // 查询Dto中的children  递归查询
    public void findChildren(UmsMenuDto umsMenuDto) {
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new LambdaQueryWrapper<UmsMenu>().eq(UmsMenu::getParentId, umsMenuDto.getId()));
        if (umsMenus.isEmpty()) {
            return;
        }
        List<UmsMenuDto> collect = umsMenus.stream().map(umsMenu -> {
            UmsMenuDto chrildMenuDto = new UmsMenuDto();
            BeanUtil.copyProperties(umsMenu, chrildMenuDto);
            findChildren(chrildMenuDto);
            return chrildMenuDto;
        }).collect(Collectors.toList());
        umsMenuDto.setChildren(collect);
    }

    @Override
    public void updateByUmsMenu(UmsMenu umsMenu) {
        umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public void updateMenuHidden(Long id, Integer hidden) {
        UmsMenu umsMenu = umsMenuMapper.selectById(id);
        umsMenu.setHidden(hidden);
        umsMenuMapper.updateById(umsMenu);
    }

}
